//
//  TestViewController10.m
//  MSDemo
//
//  Created by ms on 2024/10/30.
//

#import "TestViewController10.h"

@interface TestViewController10 ()

@property (nonatomic, strong) LotteryWheelView *lotteryWheelView;
@property (nonatomic, strong) UIView *pointerV;
@property (nonatomic, strong) UIView *fingerV;

@end

@implementation TestViewController10

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"抽奖";
    
    // 转盘背景
    UIView *bgV = [[UIView alloc] init];
    bgV.layer.contents = (id)MS_IMAGE_NAMED(@"lotteryWheel_bg").CGImage;
    [self.view addSubview:bgV];
    
    [bgV mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.mas_equalTo(660 * MS_ADAPTATION_RATIO);
        make.center.equalTo(self.view);
    }];
    
    // 转盘
    LotteryWheelView *lotteryWheelView = [[LotteryWheelView alloc] init];
    NSArray *arr = @[@{@"title" : @"标题1", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题2", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题3", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题4", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题5", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题6", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题7", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"},
                     @{@"title" : @"标题8", @"image" : @"https://inews.gtimg.com/news_bt/OIPr9G8LrCpP4K3cdWrmvalN2p2YWWB3URkibvS38awOEAA/641"}];
    lotteryWheelView.numberOfDivisions = arr.count;
    lotteryWheelView.items = arr;
    [bgV addSubview:lotteryWheelView];
    
    [lotteryWheelView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.mas_equalTo(bgV).offset(-80 * MS_ADAPTATION_RATIO);
        make.center.equalTo(bgV);
    }];
    self.lotteryWheelView = lotteryWheelView;
    // 指针
    UIView *pointerV = [[UIView alloc] init];
    pointerV.layer.contents = (id)MS_IMAGE_NAMED(@"lotteryWheel_pointer").CGImage;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(startAnimaition)];
    [pointerV addGestureRecognizer:tap];
    [bgV addSubview:pointerV];
    self.pointerV = pointerV;
    
    [pointerV mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(153 * MS_ADAPTATION_RATIO);
        make.height.mas_equalTo(179 * MS_ADAPTATION_RATIO);
        make.centerX.equalTo(bgV);
        make.centerY.equalTo(bgV).offset(-15 * MS_ADAPTATION_RATIO);
    }];
    // 抽奖次数
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 2;
    label.textAlignment = NSTextAlignmentCenter;
    NSString *count = @"199";
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"开始\n还剩%@次", count]];
    [att addAttributes:@{NSFontAttributeName : MS_SYSTEM_FONT_OF_SIZE(18), NSForegroundColorAttributeName : MS_HEX_COLOR(0xFFF3E6)} range:NSMakeRange(0, 2)];
    [att addAttributes:@{NSFontAttributeName : MS_SYSTEM_FONT_OF_SIZE(11), NSForegroundColorAttributeName : MS_HEX_COLOR(0xFFF3E6)} range:NSMakeRange(3, 2)];
    [att addAttributes:@{NSFontAttributeName : MS_SYSTEM_FONT_OF_SIZE(16), NSForegroundColorAttributeName : MS_HEX_COLOR(0xFFF3E6)} range:NSMakeRange(5, count.length)];
    [att addAttributes:@{NSFontAttributeName : MS_SYSTEM_FONT_OF_SIZE(11), NSForegroundColorAttributeName : MS_HEX_COLOR(0xFFF3E6)} range:NSMakeRange(att.length - 1, 1)];
    label.attributedText = att;
    [bgV addSubview:label];
    
    [label mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(bgV);
        make.height.mas_equalTo(90 * MS_ADAPTATION_RATIO);
    }];
    
    // 手指
    UIView *fingerV = [[UIView alloc] init];
    fingerV.layer.contents = (id)MS_IMAGE_NAMED(@"lotteryWheel_finger").CGImage;
    [bgV addSubview:fingerV];
    
    [fingerV mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(bgV.mas_centerY);
        make.left.equalTo(bgV.mas_centerX).offset(30 * MS_ADAPTATION_RATIO);
        make.width.mas_equalTo(128 * MS_ADAPTATION_RATIO);
        make.height.mas_equalTo(110 * MS_ADAPTATION_RATIO);
    }];
    self.fingerV = fingerV;
    
    // 手指动画
    CAKeyframeAnimation *fingerV_bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
    fingerV_bounceAnimation.values = @[@(380 * MS_ADAPTATION_RATIO),
                               @(380 * MS_ADAPTATION_RATIO + 50 * MS_ADAPTATION_RATIO),
                               @(380 * MS_ADAPTATION_RATIO)];
    // 设置动画时间
    fingerV_bounceAnimation.duration = 1.0;
    // 设置动画无限重复
    fingerV_bounceAnimation.repeatCount = CGFLOAT_MAX;
    // 使用弹簧效果的时间函数
    fingerV_bounceAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    // 给视图添加动画（开始动画）
    [fingerV.layer addAnimation:fingerV_bounceAnimation forKey:@"bounceAnimation"];
}

- (void)startAnimaition
{
    self.pointerV.userInteractionEnabled = NO;
    [self.fingerV.layer ms_pauseAnimation];
    self.fingerV.hidden = YES;
    [self.lotteryWheelView ms_startAnimaition];
    [self performSelector:@selector(xxxx) withObject:self afterDelay:3];
}

- (void)xxxx
{
    // 模拟概率中奖
    //控制概率
    NSInteger randomNum = arc4random()%100;
    //中奖奖品序号
    NSInteger i = 0;
    
    if (randomNum>=0 && randomNum<25)
    {
        i = 0;
    }
    else if (randomNum>=20 && randomNum<50)
    {
        i = 1;
    }
    else if (randomNum >=50 && randomNum<75)
    {
        i = 2;
    }
    else
    {
        i = 3;
    }
    
    MSLog(@"抽中奖品%@ 动画开始", self.lotteryWheelView.items[i][@"title"]);
    [self.lotteryWheelView ms_startAnimaition:i
                                     complete:^{
       
        MSLog(@"动画结束");
        self.fingerV.hidden = NO;
        [self.fingerV.layer ms_resumeAnimation];
        self.pointerV.userInteractionEnabled = YES;
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
